/*
 * @Author: your name
 * @Date: 2021-10-05 21:17:31
 * @LastEditTime: 2021-10-05 22:33:38
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \luogu\1.2排序\P1104\2main.cpp
 */
#include<iostream>
#include<stdlib.h>
using namespace std;

#define NAME_LENGTH 22
#define STU_NUM 110

typedef struct Sinfo
{
  char name[NAME_LENGTH];
  int year;
  int month;
  int day;
}sinfo;

/*
  比较两个学生年龄
  stu1 > stu2 return 1
  stu1 = stu2 return 0
  stu1 < stu2 return -1
*/
int ageCompare(const sinfo* stu1,const sinfo* stu2);
//排序 年龄 降序
void ageSort(sinfo** stu,int n);

int main(){
  int n;
  //stu管理所有的学生指针(*stu)   
  //(*stu)指向每一个学生
  sinfo** stu = NULL;
  cin >> n;
  //申请n+2个学生指针空间
  stu = (sinfo**)malloc(sizeof(sinfo*)*(n+2));
  if(!stu){
    cout << "no memory" << endl;
    exit(-1);
  }
  //为每一个学生申请一片空间,并输入该学生信息
  for(int i=1; i<=n; i++){
    //申请空间
    stu[i] = (sinfo*)malloc(sizeof(sinfo));
    //失败
    if(!stu[i]){
      cout << "no memory" << endl;
      exit(-1);
    } 
    //输入学生信息
    cin >> stu[i]->name >> stu[i]->year
      >> stu[i]->month >> stu[i]->day;
  }

  ageSort(stu,n);
  for(int i=1; i<=n; i++){
    cout << stu[i]->name << endl;
  }

  //释放堆区空间
  for(int i=1; i<=n; i++){
    if(stu[i]){
      free(stu[i]);
      stu[i] = NULL;
    }
  }
  if(stu){
    free(stu);
    stu = NULL;
  }
}

int ageCompare(const sinfo* stu1,const sinfo* stu2)
{
  //2000 10 11  2000*10000 + 10*100 + 11*1 = 20001011
  int birth1 = stu1->year*10000 + stu1->month*100 + stu1->day*1;
  int birth2 = stu2->year*10000 + stu2->month*100 + stu2->day*1;
  //birth越小 年龄越大
  if(birth1 < birth2){
    //stu1 年龄大
    return 1;
  }
  if(birth1 > birth2){
    //stu1年龄小
    return -1;
  }
  //年龄相同
  return 0;
}

void ageSort(sinfo** stu,int n)
{
  int i,j;
  //插入排序
  for(i=2; i<=n; i++){
    //stu[i]年龄大于等于stu[i-1] 进入if
    if(ageCompare(stu[i],stu[i-1]) >= 0){
      //stu[0]暂存stu[i] 交换指针
      stu[0] = stu[i];
      //将年龄小于等于stu[0]的学生后移
      for(j=i-1; ageCompare(stu[j],stu[0]) <= 0 && j >= 1; j--){
        stu[j+1] = stu[j];
      }
      //stu[0]归位 此时stu[j]的年龄大于stu[0]
      stu[j+1] = stu[0];
    }
  }
}